<template>
  <div>
    <div class="article toolbar">
      <div class="container">
        <div class="title ue-clear">
          <h2>聊天记录/文件管理</h2>
        </div>

        <div class="table-box">
          <table class="table table-bordered">
            <thead>
              <tr>
                <th>发送人</th>
                <th>接收人</th>
                <th>内容</th>
                <th>发送时间</th>
                <th>操作</th>
              </tr>
            </thead>

            <tbody>
              <tr v-for="(item, index) in dataList" :key="index">
                <td>{{ item.from.username }}</td>
                <td>
                <span v-if="item.toid=='0'">公共</span>
                   <span v-if="item.toid!='0'">{{item.to.username}}</span>
                </td>
                <td>
                <span v-if="item.isfile==0">{{ item.con }}
                </span>
                <a   v-if="item.isfile==1" :href="base+item.con" target="_blank">文件</a>
                </td>
                <td>{{ item.created }}</td>
                <td>
                  <!-- <a
                    class="btn btn-primary"
                    href="javascript:;"
                    @click="editData(item.id)"
                    >修改</a
                  >  -->
                  
                  <a 
                    class="btn btn-danger"
                    href="javascript:;"
                    @click="deleteData(item.id)"
                    >删除</a
                  >
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getList, deleteData } from "@/api/chatmsgApi";

export default {
  name: "ChatmsgList",
  data() {
    return { 
       base:process.env.VUE_APP_BASE_URL,
      dataList: []   };
  },
  created() {
    this.$nextTick(() => {
      this.getList();
    });
  },
  methods: {
    editData(id) {
      this.$router.push("/dashboard/chatmsg-edit/" + id);
    },
    deleteData(id) {
      this.$confirm("你确定要删除吗？", "确认信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          deleteData({ id: id }).then((resp) => {
            this.$message.success({
              message: "删除成功",
              type: "success",
            });
            this.getList();
          });
        })
        .catch((action) => {});
    },
    getList() {
         let account = JSON.parse(localStorage.getItem('account'))
      console.log(account.username)
      getList({}).then((resp) => {
        this.dataList = resp.data.data;
        console.log(account)
        if(account.role != 2) {
          this.dataList = resp.data.data.filter(item => {
            return item.from && item.from.username && item.from.username === account.username;
          });
        }
      });
    },
  },
  beforeDestroy() {
    this.removeFile(
      "http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css",
      "css"
    );
    this.removeFile("http://libs.baidu.com/jquery/2.0.0/jquery.min.js", "js");
    this.removeFile("source/scripts/window.js", "js");
    this.removeFile(
      "http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js",
      "js"
    );
  },
  mounted() {
    this.loadFile(
      "http://libs.baidu.com/bootstrap1/3.0.3/css/bootstrap1.min.css",
      "css"
    );
    this.loadFile("http://libs.baidu.com/jquery/2.0.0/jquery.min.js", "js");
    this.loadFile("source/scripts/window.js", "js");
    this.loadFile(
      "http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js",
      "js"
    );
  },
};
</script>
<style scoped>
</style>
